package com.zhuss.core.processor;

import java.util.Date;
import java.util.List;

import com.zhuss.core.processor.cache.CompanyCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.company.UpdateCompanyBo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.core.db.dao.ext.CompanyDao;
import com.zhuss.core.db.entity.Company;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 创建公司 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("companyUpdate")
public class CompanyUpdate extends AbstractProcessor<Object, UpdateCompanyBo> implements Processor<Object, UpdateCompanyBo> {

    @Autowired
    private CompanyDao companyDao;

    @Autowired
	@Qualifier("companyRead")
	private CompanyRead companyRead;

    @Autowired
    @Qualifier("companyCache")
    private CompanyCache companyCache;

    /**
     * 校验
     */
    @Override
    public void check(UpdateCompanyBo bo) throws DzcxException {
    	Long companyId = bo.getCompanyId();
    	String name = bo.getName();
    	if (companyId == null || companyId <= 0l) {
    		throw new DzcxException(ExceptionCode.公司ID为空);
    	}
    	if (name == null || "".equals(name.trim())) {
    		throw new DzcxException(ExceptionCode.公司name为空);
    	}

    	List<Company> list = companyRead.listNotId(name, companyId);
    	if (!list.isEmpty()) {
    		throw new DzcxException(ExceptionCode.公司name已存在);
    	}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(UpdateCompanyBo param) {
    	Company record = new Company();
    	record.setCompanyId(param.getCompanyId());
    	record.setName(param.getName());
    	record.setDutyUser(param.getDutyUser());
    	record.setDutyPhone(param.getDutyPhone());
    	record.setLinkUser(param.getLinkUser());
    	record.setLinkPhone(param.getLinkPhone());
    	record.setStatus(param.getStatus());
    	record.setSort(param.getSort());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	companyDao.updateByPrimaryKeySelective(record);

    	companyCache.clear(record.getCompanyId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateCompanyBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.基础模块.key());
    	record.setAction(LogActionEnum.公司修改.key());
    	record.setTargetId(param.getCompanyId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
